package com.pctx.bibatracker.util;

import com.pctx.bibacommon.util.HttpUtil;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;

public class CoordinateTransforToGaoDe {
    public static final String key = "066f35c54ac0eb6e6245aa418a8b8439";
    private static final Logger Log = Logger.getLogger(CoordinateTransform.class);
    private static final Double PI = Math.PI;
    private static final Double PK = 180 / PI;

    /**
     * WGS-84转换成高德地图
     *
     * @param cord_x
     * @param cord_y
     * @return
     */
    public static String getCoordinate(BigDecimal cord_x, BigDecimal cord_y) {

        String coords = cord_x.floatValue() + "," + cord_y.floatValue();
        String url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" + coords + "&coordsys=gps&output=json&key=" + key;
        String result = HttpUtil.sendHttpGetReqToServer(url);
        return result;
    }

    /**
     * 导航
     *
     * @param orign_x
     * @param orign_y
     * @param destional_x
     * @param destional_y
     * @return
     */
    public static String getNavigation(BigDecimal orign_x, BigDecimal orign_y, BigDecimal destional_x, BigDecimal destional_y) {

        String orign = orign_x.floatValue() + "," + orign_y.floatValue();
        String destional = destional_x.floatValue() + "," + destional_y.floatValue();
        String url = "http://restapi.amap.com/v3/direction/walking?origin=" + orign + "&destination=" + destional + "&key=" + key;
        String result = HttpUtil.sendHttpGetReqToServer(url);
        return result;
    }

    /**
     * 通过地址返回高德坐标
     *
     * @param address
     * @return
     */
    public static String tansferAddress(String address) {
        String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address + "&output=json&key=" + key;
        String result = HttpUtil.sendHttpGetReqToServer(url);
        return result;
    }

    /**
     * 2个经纬度坐标之间的距离
     *
     * @param lat_a
     * @param lng_a
     * @param lat_b
     * @param lng_b
     * @return
     */
    public static double getDistanceFromTwoPoints(double lat_a, double lng_a, double lat_b, double lng_b) {
        double t1 = Math.cos(lat_a / PK) * Math.cos(lng_a / PK) * Math.cos(lat_b / PK) * Math.cos(lng_b / PK);
        double t2 = Math.cos(lat_a / PK) * Math.sin(lng_a / PK) * Math.cos(lat_b / PK) * Math.sin(lng_b / PK);
        double t3 = Math.sin(lat_a / PK) * Math.sin(lat_b / PK);
        double tt = Math.acos(t1 + t2 + t3);
        System.out.println("两点间的距离：" + 6366000 * tt + " 米");
        return 6366000 * tt;
    }
}
